package com.zp.drug.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.drug.api.bo.StorageVsDeptBo;
import com.zp.drug.api.vo.StorageVsDeptVo;
import com.zp.drug.entity.StorageVsDept;
import com.zp.drug.mapper.StorageVsDeptMapper;
import com.zp.drug.service.IStorageVsDeptService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 库房单位与科室对照Service业务层处理
 *
 * @author zhang peng
 * @ date 2023-06-18
 */
@RequiredArgsConstructor
@Service
public class StorageVsDeptServiceImpl implements IStorageVsDeptService {

    private final StorageVsDeptMapper baseMapper;

    /**
     * 查询库房单位与科室对照
     */
    @Override
    public StorageVsDeptVo queryById (String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询库房单位与科室对照列表(分页)
     */
    @Override
    public TableDataInfo<StorageVsDeptVo> queryPageList (StorageVsDeptBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StorageVsDept> lqw = buildQueryWrapper(bo);
        Page<StorageVsDeptVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询库房单位与科室对照列表(不分页)
     */
    @Override
    public List<StorageVsDeptVo> queryList (StorageVsDeptBo bo) {
        LambdaQueryWrapper<StorageVsDept> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StorageVsDept> buildQueryWrapper (StorageVsDeptBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StorageVsDept> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDeptId()), StorageVsDept::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getStorageId()), StorageVsDept::getStorageId, bo.getStorageId());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), StorageVsDept::getOrgId, bo.getOrgId());
        return lqw;
    }

    /**
     * 新增库房单位与科室对照
     */
    @Override
    public Boolean insertByBo (StorageVsDeptBo bo) {
        Boolean flag = false;
        if (bo.getDeptIds().length > 0) {
            baseMapper.delete(Wrappers.<StorageVsDept>lambdaQuery()
                    .eq(StorageVsDept::getStorageId, bo.getStorageId())
                    .eq(StorageVsDept::getOrgId, bo.getOrgId())
            );
            List<StorageVsDept> list = new ArrayList<>();
            for (String deptId : bo.getDeptIds()) {
                StorageVsDept storageVsDept = new StorageVsDept();
                storageVsDept.setOrgId(bo.getOrgId());
                storageVsDept.setDeptId(deptId);
                storageVsDept.setStorageId(bo.getStorageId());
                list.add(storageVsDept);
            }
            flag = baseMapper.insertBatch(list);
        } else {
            StorageVsDept add = BeanUtil.toBean(bo, StorageVsDept.class);
            validEntityBeforeSave(add);
            flag = baseMapper.insert(add) > 0;
            if (flag) {
                bo.setId(add.getId());
            }
        }

        return flag;
    }

    /**
     * 修改库房单位与科室对照
     */
    @Override
    public Boolean updateByBo (StorageVsDeptBo bo) {
        StorageVsDept update = BeanUtil.toBean(bo, StorageVsDept.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (StorageVsDept entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除库房单位与科室对照
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
